package com.pasabahceambalaj.session;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.model.SelectItem;

import com.pasabahceambalaj.entity.*;
import com.pasabahceambalaj.session.IsemirbarkodHome.BARKODTURU;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;
import org.richfaces.model.DataProvider;
import org.richfaces.model.ExtendedTableDataModel;
import org.richfaces.model.selection.Selection;
import org.richfaces.model.selection.SimpleSelection;

@Name("isemiretutHome")
public class IsemiretutHome extends EntityHome<Isemiretut> {

	@In(create = true)
	IsemirHome isemirHome;
	@In(create = true)
	ParabirimHome parabirimHome;
	@In(create = true)
	UrunHome urunHome;
	
	@In(create = true)
	UrunList urunList;

	private Selection selection = new SimpleSelection();
	private String sortMode="single";
    private String selectionMode="single";
    private Object tableState;
    private ExtendedTableDataModel<Urun> dataModel;
    private List<Urun> uruns = new ArrayList<Urun>();
    private List<Urun> selectedUruns = new ArrayList<Urun>();


	public void setIsemiretutId(Long id) {
		setId(id);
	}

	public Long getIsemiretutId() {
		return (Long) getId();
	}

	@Override
	protected Isemiretut createInstance() {
		Isemiretut isemiretut = new Isemiretut();
		return isemiretut;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();
		Isemir isemir = isemirHome.getDefinedInstance();
		if (isemir != null) {
			getInstance().setIsemir(isemir);
		}
		Parabirim parabirim = parabirimHome.getDefinedInstance();
		if (parabirim != null) {
			getInstance().setParabirim(parabirim);
		}
		Urun urun = urunHome.getDefinedInstance();
		if (urun != null) {
			getInstance().setUrun(urun);
		}
		
		uruns = urunList.getResultList();
	}

	public boolean isWired() {
		if (getInstance().getIsemir() == null)
			return false;
		if (getInstance().getUrun() == null)
			return false;
		return true;
	}

	public Isemiretut getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}
	
	public void takeSelection() {
		//selectedUruns.clear();
		Iterator<Object> iterator = getSelection().getKeys();
		while (iterator.hasNext()) {
			Object key = iterator.next();
			//selectedUruns.add(getUrunsDataModel().getObjectByKey(key));
			this.getInstance().setUrun(getUrunsDataModel().getObjectByKey(key));
		}
	}

	public void setSelection(Selection selection) {
		this.selection = selection;
	}

	public Selection getSelection() {
		return selection;
	}

	public ExtendedTableDataModel<Urun> getUrunsDataModel() {
        if (dataModel == null) {
            dataModel = new ExtendedTableDataModel<Urun>(new DataProvider<Urun>(){

                public Urun getItemByKey(Object key) {
                    for(Urun c : uruns){
                        if (key.equals(getKey(c))){
                            return c;
                        }
                    }
                    return null;
                }

                public List<Urun> getItemsByRange(int firstRow, int endRow) {
                    return uruns.subList(firstRow, endRow);
                }

                public Object getKey(Urun item) {
                    return item.getId();
                }

                public int getRowCount() {
                    return uruns.size();
                }
                
            });
        }
        return dataModel;
    }

	public void clearSelectedUrun(){
		this.getInstance().setUrun(null);
	}
	
	public void setSortMode(String sortMode) {
		this.sortMode = sortMode;
	}

	public String getSortMode() {
		return sortMode;
	}

	public void setSelectionMode(String selectionMode) {
		this.selectionMode = selectionMode;
	}

	public String getSelectionMode() {
		return selectionMode;
	}

	public void setTableState(Object tableState) {
		this.tableState = tableState;
	}

	public Object getTableState() {
		return tableState;
	}

	
	public SelectItem[] getEtutTuru() {
		SelectItem[] items = new SelectItem[ETUTTURU.values().length];
		int i = 0;
		for (ETUTTURU g : ETUTTURU.values()) {
			items[i++] = new SelectItem(g.getCode(), g.getCode());
		}
		return items;
	}
	
	public enum ETUTTURU {
		AMB("AMB");

		private final String code;

		private ETUTTURU(String code) {
			this.code = code;
		}

		public String getCode() {
			return this.code;
		}
	}
	
}
